package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.HttpRquestUtil;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.controller.base.BaseController;
import com.jumi.microservice.domain.ExpressDO;
import com.jumi.microservice.domain.ExpressDTO;
import com.jumi.microservice.domain.ExpressVO;
import com.jumi.microservice.service.ExpressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商城物流表 前端控制器
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@RestController
@Api(tags = "商城物流表 相关接口")
@RequestMapping("/commodity/express")
public class ExpressController extends BaseController {

    @Autowired
    private ExpressService expressService;


    @ApiOperation(value = "新增数据", notes = "新增一条商城物流表数据")
    @PostMapping("/add")
    @WebLog(description = "新增商城物流")
    public ResponseResult add(@Validated @RequestBody ExpressVO express, HttpServletRequest request) {
        ExpressDTO dto = vo2Dto(express);
        dto.setCreater(HttpRquestUtil.getUserName(request));
        dto.setModifier(HttpRquestUtil.getUserName(request));
        expressService.insert(dto);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "删除数据", notes = "删除指定ID商城物流表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @DeleteMapping("/delete")
    @WebLog(description = "删除商城物流")
    public ResponseResult delete(@RequestParam String ids, HttpServletRequest request) {
        expressService.delete(ids, HttpRquestUtil.getUserName(request));
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "更新数据", notes = "更新指定ID商城物流表数据")
    @PutMapping("/update")
    @WebLog(description = "更新商城物流")
    public ResponseResult update(@Validated @RequestBody ExpressVO express, HttpServletRequest request) {
        ExpressDTO dto = vo2Dto(express);
        dto.setModifier(HttpRquestUtil.getUserName(request));
        expressService.updateEx(dto);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "启用/禁用", notes = "更新物流公司启用/禁用")
    @PutMapping("/enableStatus")
    @WebLog(description = "编辑商城物流状态")
    public ResponseResult enableStatus(Long id, Integer status, HttpServletRequest request) {
        expressService.enableStatus(id, status, HttpRquestUtil.getUserName(request));
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "查询数据", notes = "获取指定ID商城物流表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail")
    public ResponseResult<ExpressVO> detail(@RequestParam Long id) {
        ExpressDTO express = expressService.selectById(id);
        return ResultGenerator.genSuccessResult(dto2Vo(express));
    }


    @ApiOperation(value = "查询分页", notes = "获取商城物流表分页列表数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码"),
        @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/list")
    public BasePageResponse list(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            ExpressVO express) {
        LambdaQueryWrapper<ExpressDO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(express.getExpressName()), ExpressDO::getExpressName,express.getExpressName());
        wrapper.eq(express.getId()!=null, ExpressDO::getId,express.getId());
        wrapper.ge(StringUtils.isNotEmpty(express.getStartTimeAdd()), ExpressDO::getCreateTime,express.getStartTimeAdd());
        wrapper.le(StringUtils.isNotEmpty(express.getEndTimeAdd()), ExpressDO::getCreateTime,express.getEndTimeAdd());
        wrapper.ge(StringUtils.isNotEmpty(express.getStarttimeUp()), ExpressDO::getUpdateTime,express.getStarttimeUp());
        wrapper.le(StringUtils.isNotEmpty(express.getEndTimeUp()), ExpressDO::getUpdateTime,express.getEndTimeUp());
        wrapper.eq(express.getStatus()!=null, ExpressDO::getStatus,express.getStatus());
        wrapper.eq(ExpressDO::getIsDel, StatusEnum.STATUS_NO.getCode());
        wrapper.orderByDesc(ExpressDO::getId, ExpressDO::getUpdateTime);
        IPage<ExpressDO> pageInfo = expressService.page(
                new Page<>(page, size),wrapper);
        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()),pageInfo.getTotal());
    }

    /**
     * vo->dto
     * @param vo
     * @return
     */
    private ExpressDTO vo2Dto(ExpressVO vo){
        ExpressDTO dto = null;
        try {
            dto = vo.clone(ExpressDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return dto;
    }

    /**
     * dto->vo
     * @param dto
     * @return
     */
    private ExpressVO dto2Vo(ExpressDTO dto){
        ExpressVO vo = null;
        try {
            vo = dto.clone(ExpressVO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return vo;
    }

    /**
     * 列表对象转换
     * @param pos
     * @return
     */
    private List<ExpressVO> pos2Vos(List<ExpressDO> pos){
        return pos.stream().map(p->{
            ExpressVO vo = null;
            try {
               vo = p.clone(ExpressVO.class);
            }catch (Exception e){
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
    }


}
